package com.zhdl.modules.filter;

import com.project.common.util.DelayUtil;
import com.zhdl.hardware.io.smartLink.service.IOCmdSendService;
import com.zhdl.hardware.motor.huichuan.service.HCCmdSendService;
import com.zhdl.hardware_modules.service.impl.DeviceServiceImpl;
import com.zhdl.hardware_modules.web.response.ProcessStepResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class FilterProcess {

    /**
     * 压滤流程-测试模式， 流程进行
     */
    public boolean isTest = false, isRunning = false;
    /**
     * 压滤流程进度
     */
    public int processMotor = 0, sensor01Count = 0, lastSensor01Status = 0;
    List<ProcessStepResp> processStepResps;
    private final String ioNum = "30";
    @Resource
    private IOCmdSendService ioCmdSendService;
    @Resource
    private HCCmdSendService hcCmdSendService;

    /**
     * 流程开始初始化
     */
    public void init() {

        processStepResps = DeviceServiceImpl.processStepResp;

        ioCmdSendService.readAllCoil02(ioNum, bytes -> {
            /* IO 0 到位信号 4 流程开始的标记 */
            Byte io4 = bytes.get(4);
            if (!isRunning) {
                if (io4 == 1) {
                    hcCmdSendService.motorMoveDistance("8", "01", getStepParam(1, 1), getStepParam(1, 2), s -> {
                        log.info("相机拍照-拍一张右侧");
                    });
                    isRunning = true;
                }
                log.info("当前流程并非正在运行++++" + io4);
            }


            Byte io0 = bytes.get(0);
            if (io0 == 1) {
                if (lastSensor01Status == 0) {
                    sensor01Count++;
                    lastSensor01Status = 1;
                    if (sensor01Count % 2 == 0) {
                        nextIntMotor();
                    }
                }
            } else {
                lastSensor01Status = 0;
            }
        });
    }

    public int getStringParam(String param, int index) {
        String[] split = param.split(",");
        return Integer.parseInt(split[index]);
    }

    public int getStepParam(int step, int index) {
        return getStringParam(processStepResps.get(step - 1).getStepParam(), index - 1);
    }

    /**
     * 电机流程_压滤
     */
    public void nextIntMotor() {

        if (processMotor >= 43) {
            return;
        }
        processMotor++;

        if (processMotor == 43) {
            hcCmdSendService.motorMoveDistance("7", "01",
                    getStepParam(6, 1), getStepParam(6, 2), s -> {
                        log.info("电机回原点");
                        DelayUtil.delayedExecution(1000, () -> {
                            processMotor = 0;
                            isRunning = false;
                        });
                    });
        } else {
            DelayUtil.delayedExecution(1000, () ->
                    hcCmdSendService.motorMoveDistance(
                            "8", "01", getStepParam(2, 1), getStepParam(2, 2), s -> {
                                //拍正中间照片>>>
                                log.info("拍正中间照片>>>>>>>>>>>>>>>>>>>>>>>");
                                hcCmdSendService.motorMoveDistance("7", "01", getStepParam(3, 1) * processMotor, getStepParam(3, 2), s22 -> {

                                    DelayUtil.delayedExecution(1000, () ->
                                            hcCmdSendService.motorMoveDistance("8", "01", getStepParam(4, 1), getStepParam(4, 2), s1 -> {
                                                //拍左侧照片>>>
                                                log.info("相机拍照-电机移动-左 >>>>>>>>>>>>>>>>>>>>>>>");
                                                log.info("相机拍照-拍照左");
                                                DelayUtil.delayedExecution(1000, () -> {
                                                    //电机到位以后 先左边拍照 然后 右边拍照
                                                    hcCmdSendService.motorMoveDistance("8", "01", getStepParam(5, 1), getStepParam(5, 2), s11 -> {
                                                        //拍左侧照片>>>
                                                        log.info("相机拍照-电机移动-右 >>>>>>>>>>>>>>>>>>>>>>>");
                                                    });
                                                    //电机到位以后 先左边拍照 然后 右边拍照
                                                    log.info("相机拍照-拍照右");
                                                });

                                            }));

                                });

                            }));
        }

    }
}
